package com.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pojo.Drug;
import com.pojo.Inventory;
import com.service.impl.DrugServiceImpl;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * (Drug)表控制层
 *
 * @author makejava
 * @version 1.0
 * @since 2022-04-08 12:33:27
 */
@RestController
@RequestMapping("/drug")
public class DrugController {
    /**
     * 服务对象
     */
    @Resource
    private DrugServiceImpl drugServiceImpl;

    //返回管理员首页数据
    @RequestMapping("/AdminDate")
    public Map<String, Object> AdminDate(HttpServletRequest request) {
        return drugServiceImpl.AdminDate(request);
    }

    //查看库存紧张药品列表信息
    @RequestMapping("/LowerDrug")
    public PageInfo<Drug> queryLowInventoryDrugs(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                 @RequestParam("navigatePages") String navigatePages) {
        int t = Integer.parseInt(navigatePages.replace("条", ""));
        PageHelper.startPage(page, t);
        List<Drug> low = drugServiceImpl.queryLowInventoryDrugs();
        return new PageInfo<>(low, t);
    }

    //查看库存临期药品列表信息
    @RequestMapping("/overdue")
    public PageInfo<Inventory> queryClose2ExpiryPDbatches(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                          @RequestParam("navigatePages") String navigatePages) {
        int t = Integer.parseInt(navigatePages.replace("条", ""));
        PageHelper.startPage(page, t);
        List<Inventory> overdue = drugServiceImpl.queryClose2ExpiryPDbatches();
        return new PageInfo<>(overdue, t);
    }

    //查询库存药品列表
    @RequestMapping("/allDrug")
    public PageInfo<Inventory> allDrug(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                       @RequestParam("navigatePages") String navigatePages) {
        int t = Integer.parseInt(navigatePages.replace("条", ""));
        PageHelper.startPage(page, t);
        List<Inventory> all = drugServiceImpl.allDrug();
        return new PageInfo<>(all, t);
    }

    //查看库存药品批次明细
    @RequestMapping("/queryBatchOne")
    public PageInfo<Drug> queryBatchOne(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                        @RequestParam("navigatePages") String navigatePages) {
        int t = Integer.parseInt(navigatePages.replace("条", ""));
        PageHelper.startPage(page, t);
        List<Drug> drugs = drugServiceImpl.queryDrugOne();
        return new PageInfo<>(drugs, t);
    }

    /**
     * 查询所有数据
     *
     * @return 返回所有数据
     */
    @RequestMapping(value = "/selectAll")
    public Map<String, Object> selectAll() {
        return this.drugServiceImpl.selectAll();
    }

    /**
     * 通过主键删除单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @RequestMapping(value = "/deleteById")
    public Map<String, Object> deleteById(@RequestParam("id") String id) {
        return this.drugServiceImpl.deleteById(id);
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @RequestMapping(value = "/selectById")
    public Map<String, Object> selectById(String id) {
        return this.drugServiceImpl.selectById(id);
    }

    /**
     * 通过主键更新单条数据
     *
     * @param drug 一个数据库对应的POJO数据对象
     * @return 单条数据
     */
    @RequestMapping(value = "/updateById")
    public Map<String, Object> updateById(@RequestBody Drug drug) {
        return this.drugServiceImpl.updateById(drug);
    }

    /**
     * 通过一个pojo对象新增单条数据
     *
     * @param drug 一个数据库对应的POJO数据对象
     * @return 返回插入的主键id
     */
    @RequestMapping(value = "/insert")
    public Map<String, Object> insert(@RequestBody Drug drug) {
        return this.drugServiceImpl.insert(drug);
    }

}